// pinia 用户使用方法
import { defineStore } from "pinia";
export const useUserStore = defineStore({
    // 状态唯一键
    id:'user',
    // 数据（定义state的属性，可以在任何地方访问）
    state: ()=>{
        return{
            token:localStorage.getItem('token')||'',
            refreshToken:localStorage.getItem('refreshToken')||'',
            userInfo:localStorage.getItem('userInfo')||{}
        }
    },
    // 方法（用于外部调用）
    actions:{
        // state赋值
        setUserInfo(data:any){
            this.token = data.token,
            this.refreshToken = data.refreshToken,
            this.userInfo = data.userInfo
            localStorage.setItem('token',this.token)
            localStorage.setItem('refreshToken',this.refreshToken)
            localStorage.setItem('userInfo',JSON.stringify(this.userInfo))
        },
        // state清除
        clearUserState(){
            this.token=''
            this.refreshToken=''
            this.userInfo={}
            localStorage.setItem('token','')
            localStorage.setItem('refreshToken','')
            localStorage.setItem('userInfo','{}')
        }
    }

})